# -*- coding: utf-8 -*-
__author__ = 'hftec'

import math


# from load_uphi_data import U_PHI
import lcopa_usb


# lcopausb = lcopa_usb.LcopaUsb()
# lcopausb.open(0x1235)


class lcopa_vol:
    def __init__(self,uphi):
        self.uphi = uphi
        self.WAVELENGTH = 1.064
        self.PI = 3.14158265
        self.PI_2 = 6.2831653
        print "uphi",self.uphi.toString()
    def getValuge(self,phi):
        min_index=0
        min_error=10000
        if(self.uphi.U_PHI[0] >= self.uphi.U_PHI[1]):
            for cur_index in range(200):
                error = abs(self.uphi.U_PHI[cur_index]-phi);
                if(error < min_error):
                    min_error = error#uphi.U_PHI[cur_index];
                    min_index = cur_index

        else:
            for cur_index in range(200,-1,-1):
                error = abs(self.uphi.U_PHI[cur_index]-phi);
                if(error < min_error):
                    min_error = error#uphi.U_PHI[cur_index];
                    min_index = cur_index


        return min_index

    def calculate_frame(self,vlo):
        total_volate_frame = []
        A_REG = 0
        B_REG = 0
        C_REG = 0
        D_REG = 0
        for i in range(8):
            total_volate_frame.append(0x00)

        total_volate_frame.append(0xaa)
        total_volate_frame.append(0xaa)
        for frame in range(200):
            volate_frame=[]
            for i in range(self.uphi.ElectrodeNo):
                if(vlo[i] > (199-frame)):
                    volate_frame.append(1)
                else:
                    volate_frame.append(0)
            for j in range(60):
                pos = j<<4
                for i in range(0,16,2):

                    A_REG = A_REG<<1
                    #A_REG += volate_frame[(j*16+i*2)];
                    A_REG += volate_frame[958-(pos+i)]
                    B_REG = B_REG<<1
                    B_REG += volate_frame[(pos+1+i)]
                    C_REG = C_REG<<1
                    C_REG += volate_frame[(pos+961+i)]
                    D_REG = D_REG<<1
                    #//D_REG += volate_frame[(j*16+960+i*2)];
                    D_REG += volate_frame[1918-(pos+i)]
                total_volate_frame.append(A_REG)
                total_volate_frame.append(D_REG)

                total_volate_frame.append(C_REG)
                total_volate_frame.append(B_REG)

                A_REG=0
                B_REG=0
                C_REG = 0
                D_REG=0
        for d in total_volate_frame:
            print '%x'%d
        total_bytes = ""
        for i in range(48010,48032):
            total_volate_frame.append(0x00)
        for i in range(len(total_volate_frame)):
            total_bytes+=(chr(total_volate_frame[i]))
        #print 'total_volate_frame',total_volate_frame
        return total_bytes
    #lcopausb.send_byte(total_bytes)
    def calculate_voltage(self,angle):
        volate_arry = []

        k0 = 2 * self.PI / self.WAVELENGTH;
        d = (self.uphi.ElectrodeWidth*1000000+self.uphi.ElectrodeGap*1000000);
        deltaphi = k0 * d * math.sin((angle));

        for i in range(self.uphi.ElectrodeNo):
            phi = ((deltaphi * i)%self.PI_2)
            #print("phi:%4f\r\n",phi);
            volate_arry.append(self.getValuge(phi))
            # print self.getValuge(phi)
            #print("len vol",len(volate_arry))

            # write_file("./u.txt",volate_arry,1920);
        # print "volate_arry",volate_arry
        return self.calculate_frame(volate_arry)


    '''
    calculate voltage by the angle
    n_pi is the reset angle,seting by user
    '''
    def calculate_voltage_pi(self,angle,n_pi):
        volate_arry = [];

        k0 = 2 * self.PI / self.WAVELENGTH;
        d = (self.uphi.ElectrodeWidth*1000000+self.uphi.ElectrodeGap*1000000);
        deltaphi = k0 * d * math.sin((angle));

        for i in range(self.uphi.ElectrodeNo):
            phi = ((deltaphi * i)%n_pi)
            # print phi
            volate_arry.append(self.getValuge(phi))
            # print self.getValuge(phi)
        # print "volate_arry",volate_arry
        # print "volate_arry",volate_arry
        return self.calculate_frame(volate_arry)
    '''
    calculate voltage by the angle
    n_pi is the reset angle,seting by user
    '''
    def calculate_voltage2_pi(self,angle1,angle2,n_pi):
        volate_arry = [];

        k0 = 2 * self.PI / self.WAVELENGTH;
        d = (self.uphi.ElectrodeWidth*1000000+self.uphi.ElectrodeGap*1000000);
        deltaphi = k0 * d * math.sin((angle1));

        for i in range((self.uphi.ElectrodeNo>>1)):
            phi = ((deltaphi * i)%n_pi)
            #print("phi:%4f\r\n",phi);
            volate_arry.append(self.getValuge(phi))
        k0 = 2 * self.PI / self.WAVELENGTH;
        d = (self.uphi.ElectrodeWidth*1000000+self.uphi.ElectrodeGap*1000000);
        deltaphi = k0 * d * math.sin((angle2));

        for i in range((self.uphi.ElectrodeNo>>1)):
            phi = ((deltaphi * i)%n_pi)
            #print("phi:%4f\r\n",phi);
            volate_arry.append(self.getValuge(phi))
        # print "volate_arry",volate_arry
        # print "volate_arry",volate_arry
        return self.calculate_frame(volate_arry)

    def calculate_cmd(self,json_cmd):
        if(json_cmd["cmd"] == 2):#多个角度连续控制
            pass
        elif(json_cmd["cmd"] == 1):
            self.calculate_voltage(json_cmd["phi"])

